//
//  XSJHomeTableViewController.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/10.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit


private let HomeTableViewCellIdentifier = "HomeTableViewCellIdentifier"


class XSJHomeTableViewController: XSJVisitorTableViewController {
    
    
private lazy var statusListViewModel: XSJStatusListViewModel = XSJStatusListViewModel()
    
    private lazy var pullUpView : UIActivityIndicatorView = {
    
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        view.color = UIColor.orangeColor()
        return view
        
    }()
    
    private lazy var refreshCtr : UIRefreshControl = {
    
        let stc = UIRefreshControl()
        return stc
        
    }()
    
    
    private lazy var pullDownView: XSJRefreshControl = {
    
        let ctr = XSJRefreshControl()
        return ctr
    }()
    
    
    private lazy var tipLabel: UILabel = {
        let label = UILabel(textColor: UIColor.whiteColor(), fontSize: 12)
        label.textAlignment = .Center
        label.backgroundColor = UIColor.orangeColor()
        label.text = "哈哈哈哈"
        label.hidden = true
        return label
        
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if userLogin {
            setupTableView()
       
//            statusListViewModel.requestStatus(false , complete: { (isSuccess) -> () in
//                if isSuccess {
//                    self.tableView.reloadData()
//                }
            
//            })
            
            setupTableView()
            setUpui()
            
            loadStatus()
            
        } else {
           
            visitorView?.setVistorInfo(nil, imageName: nil)
        }
    }
    
    
    private func setUpui () {
    
        if let nav = self.navigationController {
            tipLabel.frame = CGRectMake(0, CGRectGetMaxY(nav.navigationBar.frame) - 35, tableView.frame.size.width, 35)
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
        }

        
    }
    
    private func loadStatus() {
        statusListViewModel.requestStatus(pullUpView.isAnimating(), complete: { (isSuccess, message) -> () in
            
            if !self.pullUpView.isAnimating() {
                
                
                self.startAnimation(message)
                
            }
            
            self.endRefreshing()
            
            if isSuccess {
                self.tableView.reloadData()
            }
        })

        
    }
    
    
    
    

    
    private func endRefreshing () {
        pullUpView.stopAnimating()
        refreshCtr.endRefreshing()
        pullDownView.endRefreshing()
        
        
    }
    
    private func setupTableView() {
        
        tableView.registerClass(XSJStatusTableViewCell.self, forCellReuseIdentifier: HomeTableViewCellIdentifier)

//        tableView.rowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        tableView.separatorStyle = .None
        tableView.tableFooterView = pullUpView
        pullUpView.sizeToFit()
        
//        refreshCtr.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
//        self.refreshControl = refreshCtr
//        tableView.addSubview(refreshCtr)
        pullDownView.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
        tableView.addSubview(pullDownView)
    }
    
    private func startAnimation(message: String) {
        
        if tipLabel.hidden == false {
            return
        }
        
        
        tipLabel.text = message
       
        tipLabel.hidden = false
        
        UIView.animateWithDuration(1, animations: { () -> Void in
            self.tipLabel.transform = CGAffineTransformMakeTranslation(0, self.tipLabel.frame.size.height)
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                  
                    self.tipLabel.transform = CGAffineTransformIdentity
                    
                    }, completion: { (_) -> Void in
                        self.tipLabel.hidden = true
                })
                
        }
        
    }

}

func testRefresh(ctr: XSJTestRefreshControl) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
        ctr.endRefreshing()
    })
}

extension XSJHomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeTableViewCellIdentifier, forIndexPath: indexPath) as! XSJStatusTableViewCell
        cell.statusViewModel = statusListViewModel.statusList![indexPath.row]
        cell.selectionStyle = .None
        return cell
        
    }

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == (statusListViewModel.statusList?.count)! - 1 && !pullUpView.isAnimating()
        {
        pullUpView.startAnimating()
            loadStatus()
//            print("我是最后一条数据了")
            
        }
    }
    
    @objc private func pullDownRefresh () {
    
        loadStatus()
        
        
    }
}
